package com.example.extenddemo.mail;

import jakarta.annotation.Resource;
import jakarta.mail.internet.MimeMessage;
import lombok.SneakyThrows;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.io.File;

@Service
public class MailService {

    @Resource
    private JavaMailSender javaMailSender;
    /**
     * @Description: <span style="color:'green'">发送邮件服务</span>
     * @param form 发送人邮箱地址
     * @param to 接收人邮箱地址
     * @param context 邮件内容
     * @param subject 邮件主题
     * @param file：附件
     */
    @SneakyThrows
    public boolean sendMail(String form, String to, String context,String subject, File file){
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(form);
        helper.setTo(to);
        helper.setCc(form);
        helper.setText(context);
        helper.setSubject(subject);
        helper.addAttachment(file.getName(), file);
        javaMailSender.send(message);
        return true;
    }
}
